package com.yxw.live_vod_boot.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxw.live_vod_boot.entity.YxLiveAddressEntity;
import com.yxw.live_vod_boot.entity.YxLiveShopEntity;
import com.yxw.live_vod_boot.entity.YxOnDemandVideoEntity;
import com.yxw.live_vod_boot.entity.YxSysShareEntity;
import com.yxw.live_vod_boot.mapper.OnDemandVideoMapper;
import com.yxw.live_vod_boot.mapper.ShareMapper;
import com.yxw.live_vod_boot.mapper.ShopMapper;
import com.yxw.live_vod_boot.mapper.YxLiveAddressMapper;
import com.yxw.live_vod_boot.service.SessionTalkService;
import com.yxw.live_vod_boot.service.ShareService;
import com.yxw.live_vod_boot.utils.JsonUtils;
import com.yxw.live_vod_boot.utils.RedisManager;
import com.yxw.live_vod_boot.utils.UserUtil;
import com.yxw.live_vod_facade.constant.LiveShopConstant;
import com.yxw.live_vod_facade.dto.OnSiteShareDto;
import com.yxw.live_vod_facade.dto.OnSiteShareUserDto;
import com.yxw.live_vod_facade.vo.OnSiteShareVo;
import com.yxw.yxnet_cd_center.common.constant.CommonConstant;
import com.yxw.yxnet_cd_center.common.exception.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;

/**
 * @author liuxin
 * 2023.10.07
 * 分享管理
 */
@Service
public class ShareServiceImpl extends ServiceImpl<ShareMapper,YxSysShareEntity> implements ShareService {


    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private OnDemandVideoMapper onDemandVideoMapper;

    @Autowired
    private YxLiveAddressMapper yxLiveAddressMapper;

    @Autowired
    private UserUtil userUtil;

    @Autowired
    private SessionTalkService sessionTalkService;
    
    @Resource
    private RedisManager redisManager;

    /**
     * 站内直播点播分享
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void onSiteShare(OnSiteShareDto dto) {
        //获取登陆人
        Long userId = userUtil.getLoginUserId();
        //前端分享内容实体
        OnSiteShareVo vo=new OnSiteShareVo();
        vo.setShareType(dto.getShareType());
        Long liveShopId = 0L;
        if(CommonConstant.ONE==dto.getShareType()){
            //查询点播分享信息
            YxOnDemandVideoEntity vodEntity = onDemandVideoMapper.selectById(dto.getBussId());
            if(null==vodEntity){
                throw new BaseException("分享视频不存在");
            }
            vo.setBussId(String.valueOf(vodEntity.getVideoId()));
            vo.setCoverUrl(vodEntity.getCoverUrl());
            vo.setPalyUrl(vodEntity.getPlayUrl());
            liveShopId = vodEntity.getShopId();
        }else{
            //直播信息查询
            YxLiveAddressEntity liveEntity = yxLiveAddressMapper.selectById(dto.getBussId());
            if(null==liveEntity){
                throw new BaseException("分享直播间不存在");
            }
            vo.setBussId(liveEntity.getId());
            vo.setPalyUrl(liveEntity.getLiveUrl());
            vo.setCoverUrl(liveEntity.getLivePicture());
            //查询店铺信息
            YxLiveShopEntity shopEntity = shopMapper.selectById(liveEntity.getShopId());
            if(null==shopEntity){
                throw new BaseException("店铺不存在");
            }
            vo.setShopId(shopEntity.getShopId());
            vo.setShopName(shopEntity.getShopName());
            vo.setShopHeadUrl(shopEntity.getShopBackground());
            liveShopId = shopEntity.getLiveShopId();
            
        }
        //保存分享信息
        List<OnSiteShareUserDto> shareUserList = dto.getShareUserList();

        String loginName = userUtil.getLoginName();
        for (OnSiteShareUserDto userDto: shareUserList) {
            YxSysShareEntity shareEntity = new YxSysShareEntity();
            shareEntity.setBussId(vo.getBussId());
            shareEntity.setCreateTime(new Date());
            shareEntity.setUserId(userId);
            shareEntity.setUserName(loginName);
            shareEntity.setShareUserId(userDto.getShareUserId());
            shareEntity.setShareUserName(userDto.getShareUserName());
            shareEntity.setShareUserType(userDto.getShareUserType());
            shareEntity.setShareType(vo.getShareType());
            shareEntity.setLiveShopId(liveShopId);
            shareEntity.setShareContent(JsonUtils.toJson(vo));
            baseMapper.insert(shareEntity);
            //分享会话发送
            sessionTalkService.sendTalkByShare(shareEntity);
        }
        if(CommonConstant.TWO.intValue() == dto.getShareType().intValue())
        {
            String redisKey = LiveShopConstant.LIVE_SHOP_USER_SHARE_HASHMAP_PREFIX.concat(userId.toString());
            redisManager.setMapValue(redisKey, liveShopId.toString(), liveShopId.toString());
        }
        
    }
}
